﻿#include "pch.h"
#include "read.h"
using namespace std;
any read::datachange(string out, vector<string> datatype, int datacol) {
	if (datatype[datacol] == "int") {
		return stoi(out);
	}
	if (datatype[datacol] == "long") {
		return stol(out);
	}
	if (datatype[datacol] == "float") {
		return stof(out);
	}
	if (datatype[datacol] == "double") {
		return stod(out);
	}
	if (datatype[datacol] == "string") {
		return out;
	}
	return 0;
}
vector<vector<any>> read::readf(string p_String,vector<vector<any>> &dataoutput, vector<string> &outputtype)//dataoutput 在引用中
{
	fstream  f(p_String.c_str());//读取文件
	string line;//读取的每一行
	string out;//每一行以空格隔开的字符串
	string isover;//判断是否到达dataoutput的字符串
	vector<vector<any>> datainput;
	//vector<int> inputsize; //接收datainput的大小
	//vector<int> outputsize;
	vector<string> inputtype;//接收input的数据类型
	getline(f, line);
	line = line.substr(0, 5);
	if (line == "input") {//读input部分
		//getline(f, line);//接收datainput的大小，目前来看没多大用
		//stringstream input(line);
		//while (input.good()) {
		//	getline(input, out, ' ');
		//	if (!out.empty())
		//		inputsize.push_back(atoi(out.c_str()));
		//}
		getline(f, line);
		stringstream input2(line);
		while (input2.good()) {//接收inputtype
			getline(input2, out, ' ');
			if (!out.empty())
				inputtype.push_back(out);
		}
		while (getline(f, line))
		{
			vector<any> tem;
			isover = line.substr(0, 6);
			if (isover == "output")break;//如果到了output部分，跳出循环
			stringstream input(line);
			int col = 0;
			while (input.good()) {//接收input 二维向量内数据
				getline(input, out, ' ');
				if (!out.empty()) {
					any anytem = datachange(out, inputtype, col);
					tem.push_back(anytem);
				}
				col++;
			}
			datainput.push_back(tem);
		}

	}
	//getline(f, line);
	//stringstream input(line);
	//while (input.good()) {
	//	getline(input, out, ' ');
	//	if (!out.empty())
	//		outputsize.push_back(atoi(out.c_str()));
	//}
	getline(f, line);//读output部分
	stringstream input2(line);
	while (input2.good()) {//接收outputtype
		getline(input2, out, ' ');
		if (!out.empty())
			outputtype.push_back(out);
	}
	while (getline(f, line))
	{
		vector<any> tem;
		stringstream input(line);
		int col = 0;
		while (input.good()) {//接收output 二维向量内数据
			getline(input, out, ' ');
			if (!out.empty()) {
				any anytem = datachange(out, outputtype, col);
				tem.push_back(anytem);
			}
			col++;
		}
		dataoutput.push_back(tem);
	}
	return datainput;//返回input
}
